   package com.javalong.affairs.base.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.javalong.affairs.base.common.GlobalApplication;
import com.javalong.affairs.base.common.ServiceLocator;
import com.javalong.affairs.base.common.UIHandler;
import com.javalong.affairs.base.listener.DefaultErrorListener;
import com.javalong.affairs.base.listener.DefaultResponseListener;

public class HttpUtils {
	private static final String TAG = "HttpUtils";
	private static UIHandler uiHandler = null;
	private static RequestQueue requestQueue;
	static {
		uiHandler = ServiceLocator.GetInstance().getInstance(UIHandler.class);
		requestQueue = Volley.newRequestQueue(GlobalApplication.getInstance());
	}

	private HttpUtils() {
	}

	public static void post(String url, final Map<String, Object> paramMap) {
		post(url, paramMap, new DefaultResponseListener(),
				new DefaultErrorListener());
	}

	public static void post(String url, final Map<String, Object> paramMap,
			Response.Listener<String> responseListener) {
		post(url, paramMap, responseListener, new DefaultErrorListener());
	}

	public static void post(String url, final Map<String, Object> paramMap,
			Response.Listener<String> responseListener,
			ErrorListener errorListener) {
		StringRequest stringRequest = new StringRequest(Method.POST, url,
				responseListener, errorListener) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> param = new HashMap<String, String>();
				if (paramMap != null) {
					for (Entry<String, Object> e : paramMap.entrySet()) {
						if (e.getValue() != null) {
							param.put(e.getKey(), e.getValue().toString());
						}
					}
				}
				return param;
			}

			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				Map<String, String> headerMap = new HashMap<String, String>();
				headerMap.put("Cookie", CookieUtils.getSessionHeader());
				headerMap.put("Content-Type",
						"application/x-www-form-urlencoded");
				return headerMap;
			}

			@Override
			protected Response<String> parseNetworkResponse(
					NetworkResponse response) {
				String sessionId = response.headers.get("Set-Cookie");
				if (sessionId != null) {
					String[] strs = sessionId.split("[A-Z]*=|;");
					if (strs.length > 1) {
						CookieUtils.setSessionHeader(strs[1]);
					}
				}
				return super.parseNetworkResponse(response);
			}
		};

		stringRequest.setRetryPolicy(new DefaultRetryPolicy(50000, 0, 1.0f));
		requestQueue.add(stringRequest);
	}

	public static void addReuqest(Request<JSONObject> request) {
		requestQueue.add(request);
	}

}